<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="包间号/桌号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="包间号/桌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <el-form-item label="小时价格" prop="housPrice">
	    <el-input
	      v-model="queryParams.housPrice"
	      placeholder="小时价格"
	      clearable
	      @keyup.enter.native="handleQuery"
	    />
	  </el-form-item>
      <el-form-item label="状态" prop="chessStatus">
        <el-select v-model="queryParams.chessStatus" placeholder="棋牌室状态" clearable>
          <el-option
            v-for="dict in chessStatus"
            :key="dict.type"
            :label="dict.name"
            :value="dict.type"
          />
        </el-select>
      </el-form-item>
	  <el-form-item label="类型" prop="chessType">
	    <el-select v-model="queryParams.chessType" placeholder="棋牌室类型" clearable>
	      <el-option
	        v-for="dict in chessType"
	        :key="dict.type"
	        :label="dict.name"
	        :value="dict.type"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="包间类型" prop="prRoomType">
	    <el-select v-model="queryParams.prRoomType" placeholder="包间类型" clearable>
	      <el-option
	        v-for="dict in prRoomType"
	        :key="dict.type"
	        :label="dict.name"
	        :value="dict.type"
	      />
	    </el-select>
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="序号" align="center" prop="id" width="100" />
      <el-table-column label="包间号/桌号" align="center" prop="no" width="100" />
      <el-table-column label="包间类型" align="center" prop="chessTypeCn" />
      <el-table-column label="物联设备ID" align="center" prop="deviceId" />
      <el-table-column label="小时价格" align="center" prop="housPrice" />
      <el-table-column label="状态" align="center" prop="chessStatusCn" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
			<el-col :span="24">
			  <el-form-item label="包间号/桌号" prop="no">
			    <el-input v-model="form.no" placeholder="请输入包间号/桌号" />
			  </el-form-item>
			</el-col>
			<el-col :span="24">
			  <el-form-item label="包间类型" prop="chessType">
			    <el-select v-model="form.chessType" placeholder="请选择类型" class="width100">
			      <el-option
			        v-for="dict in chessType"
			        :key="dict.type"
			        :label="dict.name"
			        :value="dict.type"
			      ></el-option>
			    </el-select>
			  </el-form-item>
			</el-col>
			<el-col :span="24">
			  <el-form-item label="包间大小" prop="prRoomType">
				  <el-select v-model="form.prRoomType" placeholder="请选择包间大小" class="width100">
				    <el-option
				      v-for="dict in prRoomType"
				      :key="dict.type+''"
				      :label="dict.name"
				      :value="dict.type+''"
				    ></el-option>
				  </el-select>
			  </el-form-item>
			</el-col> 
			<el-col :span="24">
			  <el-form-item label="状态" prop="chessStatus">
				  <el-select v-model="form.chessStatus" placeholder="状态" class="width100">
				    <el-option
				      v-for="dict in chessStatus"
				      :key="dict.type+''"
				      :label="dict.name"
				      :value="dict.type+''"
				    ></el-option>
				  </el-select>
			  </el-form-item>
			</el-col> 
			<el-col :span="24">
			  <el-form-item label="物联设备ID" prop="deviceId">
			    <el-input v-model="form.deviceId" placeholder="请输入物联设备ID" />
			  </el-form-item>
			</el-col>
			<el-col :span="24">
			  <el-form-item label="小时价格" prop="housPrice">
			    <el-input v-model="form.housPrice" placeholder="请输入小时价格" />
			  </el-form-item>
			</el-col>
          <el-col :span="24">
            <el-form-item label="介绍" prop="remark">
              <el-input v-model="form.remark" placeholder="介绍" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { 
	listChessAll,
	delChessRoom,
	insertChessRoom,
	editChessRoom,
	chessRoomList,
	detailChessRoom
} from "@/api/chesscard/index";
export default {
  name: "chesscard",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //appDays: undefined,
        chessStatus: undefined,
        chessType: undefined,
        //deviceId: undefined,
        housPrice: undefined,
        no: undefined,
        prRoomType: undefined,
        id: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        no: [
          { required: true, message: "包间号/桌号不能为空", trigger: "blur" }
        ],
		deviceId: [
		  { required: true, message: "设备ID不能为空", trigger: "blur" }
		],
		prRoomType: [
		  { required: true, message: "包间大小类型不能为空", trigger: "change" }
		],
        chessType: [
          { required: true, message: "包间类型不能为空", trigger: "change" }
        ],
		chessStatus: [
		  { required: true, message: "包间状态不能为空", trigger: "change" }
		],
		housPrice: [
		  { required: true, message: "小时价格不能为空", trigger: "blur" }
		], 
		remark: [
		  { required: true, message: "介绍不能为空", trigger: "blur" }
		],
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
  	  chessType: {
  	    get() {
  	      return this.$store.state.common.chessType
  	    },
  	  },
	  prRoomType: {
	    get() {
	      return this.$store.state.common.prRoomType
	    },
	  },
	  chessStatus: {
	    get() {
	      return this.$store.state.common.chessStatus
	    },
	  }
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      chessRoomList(this.queryParams).then(response => {
        this.noticeList = response.rows.map(i=>{
			i.chessTypeCn = this.chessType.filter(v=>v.type == i.chessType)[0].name
			i.chessStatusCn = this.chessStatus.filter(v=>v.type == i.chessStatus)[0].name
			return i
		})
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        noticeStatus: undefined,
        noticeType: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加棋牌室";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row;
      this.open = true;
      this.title = "修改棋牌室";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editChessRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertChessRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = {
      		  id:row.id
      };
      this.$modal.confirm('是否确认删除该棋牌室？').then(function() {
        return delChessRoom(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
